package com.whfc.wxmp.api.sys.service.impl;

import com.whfc.emp.dto.AppEmpDTO;
import com.whfc.emp.entity.WorkDailyReport;
import com.whfc.emp.service.AppEmpService;
import com.whfc.emp.service.AppWorkPlanService;
import com.whfc.emp.service.WorkDailyReportService;
import com.whfc.entity.dto.Apply.rep.DailyReportDisplayRep;
import com.whfc.entity.dto.Apply.rep.QueryEmpSysIdDto;
import com.whfc.entity.dto.Apply.req.AppReportRrq;
import com.whfc.entity.dto.Apply.req.WorkDailyReportQueryReq;
import com.whfc.fuum.entity.WxUser;
import com.whfc.fuum.service.SysUserService;
import com.whfc.wxmp.api.sys.service.WxDailyReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class WxDailyReportServiceImpl implements WxDailyReportService {

    @DubboReference(interfaceClass = WorkDailyReportService.class, version = "1.0.0", timeout = 30000)
    private WorkDailyReportService workDailyReportService;
    @DubboReference(interfaceClass = SysUserService.class, version = "1.0.0", timeout = 30000)
    private SysUserService sysUserService;

    @DubboReference(interfaceClass = AppEmpService.class, version = "1.0.0", timeout = 30000)
    private AppEmpService appEmpService;
    @Override
    public int saveReport(WxUser user, AppReportRrq req) {
        WorkDailyReport report = new WorkDailyReport();
        report.setPlanId(req.getPlanId());
        report.setReporter(user.getId());
        report.setTeamId(req.getDeptId());
        report.setContent(req.getContent());
        int i = workDailyReportService.addReportSelective(report);
        return i;
    }

    @Override
    public List<DailyReportDisplayRep> queryReport(WorkDailyReportQueryReq req) {
        List<DailyReportDisplayRep> displayReps = workDailyReportService.queryHarvestReport(req);
        for (DailyReportDisplayRep dailyReportDisplayRep : displayReps) {
            // 提交日报人Id
            String dailyReportId = dailyReportDisplayRep.getDailyReportId();
            List<QueryEmpSysIdDto> appEmpIds = sysUserService.getAppEmpEmpNamesIdList(Arrays.asList(dailyReportId));
            AppEmpDTO empInfo = appEmpService.getEmpInfo(appEmpIds.get(0).getAppEmpId());
            dailyReportDisplayRep.setDailyReportName(empInfo.getEmpName() + "的日報");
            dailyReportDisplayRep.setAvatarURL(empInfo.getAvatar());

        }

        return displayReps;
    }

    @Override
    public List<DailyReportDisplayRep> myDailyReport(WxUser user, WorkDailyReportQueryReq req) {
        req.setUserId(user.getId());
        List<DailyReportDisplayRep> displayReps = workDailyReportService.queryMyDailyReport(req);
        for (DailyReportDisplayRep dailyReportDisplayRep : displayReps) {
            // 提交日报人Id
            String dailyReportId = dailyReportDisplayRep.getDailyReportId();
            List<QueryEmpSysIdDto> appEmpIds = sysUserService.getAppEmpEmpNamesIdList(Arrays.asList(dailyReportId));
            AppEmpDTO empInfo = appEmpService.getEmpInfo(appEmpIds.get(0).getAppEmpId());
            dailyReportDisplayRep.setAvatarURL(empInfo.getAvatar());
        }
        return displayReps;
    }
}
